﻿#region Imports

using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using Spring.Data.NHibernate;
using Spring.Data.NHibernate.Generic.Support;
using d3PrtDgn.Core.BaseData;
using d3PrtDgn.Data.IDAO;
using d3PrtDgn.Data.Domain.Entity;

#endregion

namespace d3PrtDgn.Data.DAO
{
	/// <summary>
	/// Hibernate PhotoType DAO
	/// </summary>
	class PhototypeDao : BaseDao, IPhototypeDao
	{

		#region IPhototypeDao members

		/// <summary>
		/// Load photo type according to MIME type
		/// Creates photo type if not exists MIME
		/// </summary>
		/// <param name="type">Photo type with specified MIME type</param>
		/// <returns>Loaded or created photo type</returns>
		public Phototype FindByMimeOrCreate(Phototype type)
		{
			IList<Phototype> found = HibernateTemplate.Find<Phototype>("from Phototype pt where pt.Mime = ?", type.Mime);
			if (found.Count == 1)
				return found[0];
			else
			{
				HibernateTemplate.Save(type);
				return type;
			}
		}

		#endregion
	}
}
